local thbattle__huiye_yongye = fk.CreateSkill {

  name = "thbattle__huiye_yongye",

  tags = {  },

}

U = require "packages/utility/utility"


thbattle__huiye_yongye:addEffect(fk.AfterCardsMove, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(thbattle__huiye_yongye.name) or player:isNude() or player.phase ~= Player.NotActive then return false end
    if #table.filter(player:getCardIds("he"), function(cid)
          local card = Fk:getCardById(cid)
          return card.color == Card.Red and (card.type == Card.TypeBasic or card.type == Card.TypeEquip)
        end) == 0 then
      return false
    end
    local room = player.room
    local move_event = room.logic:getCurrentEvent()
    local parent_event = move_event.parent
    local card_ids = {}
    if parent_event ~= nil then
      if parent_event.event == GameEvent.UseCard then
        local parent_data = parent_event.data
        if parent_data.from ~= player then
          event:setCostData(self, parent_data.from.id)
          card_ids = room:getSubcardsByRule(parent_data.card)
        end
      end
    end
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        if #card_ids > 0 then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.Processing and table.contains(card_ids, info.cardId) and
                Fk:getCardById(info.cardId).color == Card.Red and Fk:getCardById(info.cardId).type == Card.TypeBasic then
              if room:getPlayerById(event:getCostData(self)) and #room:getPlayerById(event:getCostData(self)):getCardIds("j") > 0 and
                  table.find(room:getPlayerById(event:getCostData(self)):getCardIds("j"), function(cid)
                    return Fk:getCardById(cid).name == "fengmozhen__indulgence" or
                        Fk:getCardById(cid).skillName == thbattle__huiye_yongye.name
                  end) then
                return false
              else
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if not room:askForSkillInvoke(player, thbattle__huiye_yongye.name) then
      return false
    end
    local RedBasic_cids = room:askForCard(player, 1, 1, true, thbattle__huiye_yongye.name, true,
      ".|.|heart,diamond|hand,equip|.|^trick",
      "#thbattle__huiye_yongye-choose_card:" .. room:getPlayerById(event:getCostData(self)).id)
    if #RedBasic_cids > 0 then
      local target_id = event:getCostData(self)
      event:setCostData(self, { RedBasic_cids, target_id })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self)[1][1]
    local to = room:getPlayerById(event:getCostData(self)[2])
    local card = Fk:cloneCard("fengmozhen__indulgence")
    card:addSubcard(cards)
    card.skillName = thbattle__huiye_yongye.name
    if not player:canUseTo(card,to) then return end
    room:useCard({
      from = player,
      tos = {to},
      card = card,
    })
  end,
})

return thbattle__huiye_yongye